import time
from bs4 import BeautifulSoup
from selenium import webdriver
from eastmoney import config as cfg
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()

driver.get('http://quote.eastmoney.com/web/BK05811.html')
para = 'body > div:nth-child(1) > div:nth-child(8) > div.report > div.main_box > div > div.RMBCont > div > div > p > a'
wait = WebDriverWait(driver, 30)
view_more = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, para)))
view_more.click()
all_handle = driver.window_handles

# title = wait.until(lambda driver:driver.title)
# url = wait.until(lambda driver:driver.current_url)
# print(driver.current_window_handle, title, url)

driver.switch_to.window(all_handle[-1])

def get_each_concept(driver):
    td_label = wait.until(lambda driver: driver.find_elements_by_css_selector('#dt_1 > tbody > tr > td > a'))
    detail_list = []
    temp = []
    num = 0
    for tr in td_label:
        num += 1
        if num < 3:
            temp.append(tr.get_attribute('text'))
        elif num == 6:
            detail_list.append(temp)
            temp = []
            num = 0
        else:
            continue
    return detail_list


try:
    all_page = wait.until(lambda driver: driver.find_elements_by_css_selector('#PageCont > a'))
    page_count = int(all_page)-2
    for page in range(1, page_count):
        input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#PageContgopage')))
        submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#PageCont > a.btn_link')))
        input.clear()
        input.send_keys(page + 1)
        submit.click()
        detail_list = get_each_concept(driver)
        print(detail_list)

except Exception as err:
    detail_list = get_each_concept(driver)
    print(detail_list, '1111111111111111111111111111111111111111111111111')



# driver.close()